#include "client.h"
#include "dataStruct.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <iostream>
#include <thread>
#include <vector>
#include <mysql/mysql.h>
#include <unordered_map>
#include <pthread.h>
#include <set>
#include <fstream>
#include<sys/epoll.h>
#include<errno.h>

using namespace std;

namespace tally
{


TallyClient::TallyClient(std::string addr, short port) : m_addr(addr), m_port(port), m_socket(-1)
{
    // 定义 sockfd
    m_socket = socket(AF_INET, SOCK_STREAM, 0);
    
    // 定义 sockaddr_in
    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(m_port);  //服务器端口
    servaddr.sin_addr.s_addr = inet_addr(m_addr.c_str());  //服务器ip

    // 连接服务器，成功返回0，错误返回-1
    if (connect(m_socket, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("connect");
        exit(1);
    }

    m_msgs.push_back("连接服务器成功");
    while (1)
    {
        welcomInterface();
        cout << "> ";
        char c = '\0';
        cin >> c;
        switch (c)
        {
        case '1':
            userLoginInterface();
            break;
        case '2':
            userRegistInterface();
            break;
        case 'q':
        case 'Q':
            cout << "程序退出\n";
            return;        
        default:
            break;
        }
    }
}

TallyClient::~TallyClient()
{
    close(m_socket);
}

void TallyClient::welcomInterface()
{
    system("clear");
    cout << " --------------------------\n";
    cout << "| 欢迎使用云记账客户端    |\n";
    cout << "|--------------------------\n";
    cout << "|                         |\n";
    cout << "| 请输入你要的选项:       |\n";
    cout << "|    1: 登录              |\n";
    cout << "|    2: 注册              |\n";
    cout << "|    q: 退出              |\n";
    cout << "|-------------------------|\n\n";
    for(auto d: m_msgs) {
        if(!d.empty()) {
            cout << "| " << d << "   |\n";
            cout << " --------------------------\n\n";
        }
    }

    m_msgs.clear();
}

void TallyClient::userRegistInterface()
{
    system("clear");
    cout << " --------------------------\n";
    cout << "| 欢迎使用云记账客户端    |\n";
    cout << "|--------------------------\n";
    cout << "|                         |\n";
    cout << "| 请输入以下注册信息:       |\n";
    cout << "|    1: 用户名             |\n";
    cout << "|    2: 密  码             |\n";
    cout << "|    3: 邮  箱             |\n";
    cout << "|    4: 电话号码           |\n";
    cout << "|-------------------------|\n\n";

    ReginstInfo info;
    //struct ReginstInfo info;
    cout << "请输入用户名：";
    cin >> info.name;
    cout << "请输入密码：";
    cin >> info.passwd;
    cout << "请输入邮箱：";
    cin >> info.email;
    cout << "请输入电话号码：";
    cin >> info.email;
    
    send(m_socket, &info, sizeof(info), 0);

    //绑定回传的消息部分
    ReturnResult ret;
    memset(&ret, 0, sizeof(ret));
    recv(m_socket, &ret, sizeof(ret), 0);
    m_msgs.push_back(std::string(ret.info));
}

void TallyClient::userLoginInterface()
{   system("clear");
    LoginInfo info;
    cout << "请输入用户名：\n";
    cin >> info.name;
    cout << "请输入密码：\n";
    cin >> info.passwd;
    send(m_socket, &info, sizeof(info), 0);
    
    //设定该会话的消息回传接口
    ReturnResult ret;
    memset(&ret, 0, sizeof(ret));
    recv(m_socket, &ret, sizeof(ret), 0);
    m_msgs.push_back(std::string(ret.info));
    //功能接口（登录成功之后）
    if(ret.ReturnResult==false){
        return;
    }
    this.tallyRecordInterface();
}

            // struct TallyInfo
            // {
            //     TallyType       type;       // 记录类型
            //     float           money;      // 记录金额
            //     char            account[64];// 账户类型，现金、xx 银行卡、微信、支付宝...
            //     char            useage[128];// 收支用途：吃饭、买衣服、买零食、医疗、电子产品...
            //     time_t          time;       // 收支时间
            // };

void TallyClient::tallyRecordInterface()
{   
    system("clear");

    cout << " --------------------------\n";
    cout << "|  欢迎使用云记账功能       |\n";
    cout << "|--------------------------\n";
    cout << "|    请输入以下信息:       |\n";
    cout << "|    1: 记录类型           |\n";
    cout << "|    2: 记录金额           |\n";
    cout << "|    3: 账户类型           |\n";
    cout << "|    4: 收支用途           |\n";
    cout << "|    5: 收支时间           |\n";
    cout << "|--------------------------|\n\n";

    TallyInfo info;
    cout << "请输入记录类型：";
    cin >> info.type;
    cout << "请输入记录金额：";
    cin >> info.money;
    cout << "请输入账户类型：";
    cin >> info.account;
    cout << "请输入收支用途：";
    cin >> info.useage;
    cout << "请输入收支时间：";
    cin >> info.time;
    //通信部分
    
    send(m_socket, &info, sizeof(info), 0);

    //绑定回传的消息部分
    ReturnResult ret;
    memset(&ret, 0, sizeof(ret));
    recv(m_socket, &ret, sizeof(ret), 0);
    m_msgs.push_back(std::string(ret.info));
}


} // end of namespace